package com.yyoo.doc.html.style;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 像素大小转换为 int 类型
 * 转换后的数字单位为 px
 *
 * @author zzy
 */
public class ColorConverter extends StyleValueConverter<Color>{

    private static final Map<String, Color> colorMap = new HashMap<>();

    /**
     * 支持的样式列表
     */
    private static final Map<String,Color> SUPPORT_STYLES = new HashMap<>();

    static {
        // 支持的样式及其默认值
        SUPPORT_STYLES.put(StyleAttr.COLOR,Color.black);
        SUPPORT_STYLES.put(StyleAttr.BORDER_COLOR,null);
        SUPPORT_STYLES.put(StyleAttr.BORDER_TOP_COLOR, null);
        SUPPORT_STYLES.put(StyleAttr.BORDER_BOTTOM_COLOR,null);
        SUPPORT_STYLES.put(StyleAttr.BORDER_LEFT_COLOR,null);
        SUPPORT_STYLES.put(StyleAttr.BORDER_RIGHT_COLOR,null);
        SUPPORT_STYLES.put(StyleAttr.BACKGROUND_COLOR,null);

        // 默认的颜色 map
        colorMap.put("red", Color.red);
        colorMap.put("blue", Color.blue);
        colorMap.put("green", Color.green);
        colorMap.put("black", Color.black);
        colorMap.put("white", Color.white);
        colorMap.put("yellow", Color.yellow);
        colorMap.put("orange", Color.orange);
        colorMap.put("gray", Color.gray);
        colorMap.put("pink", Color.pink);
        colorMap.put("cyan", Color.cyan);
        colorMap.put("magenta", Color.magenta);
        colorMap.put("darkgray", Color.darkGray);
        colorMap.put("lightgray", Color.lightGray);
    }

    @Override
    public boolean support(String styleKey) {
        return SUPPORT_STYLES.containsKey(styleKey);
    }

    @Override
    protected Color defaultValue(String styleKey) {
        return SUPPORT_STYLES.get(styleKey);
    }

    @Override
    protected Color convert(String styleValue) {

        if(styleValue == null || "".equalsIgnoreCase(styleValue)){
            return null;
        }

        Color color;
        try {
            color = colorMap.get(styleValue.toLowerCase());

            if(color == null) {
                // 十六进制转换（如：#000000、x000000）
                color = Color.decode(styleValue);
            }
        }catch (Exception e){
            color = null;
        }

        if(color == null){
            // rgb(255,0,0) 格式的转换
            color = parseRGB(styleValue);
        }

        // color == null 不支持的颜色样式值，将返回 defaultValue 方法默认值

        return color;
    }


    /**
     * rgb(255,0,0) 格式的转换
     * @param rgbString rgb 格式的字符串，如：rgb(255,255,255)
     * @return 返回 Color 对象
     */
    private Color parseRGB(String rgbString) {
        Pattern pattern = Pattern.compile("rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)");
        Matcher matcher = pattern.matcher(rgbString.toLowerCase());

        if (matcher.matches()) {
            int red = Integer.parseInt(matcher.group(1));
            int green = Integer.parseInt(matcher.group(2));
            int blue = Integer.parseInt(matcher.group(3));

            return new Color(red, green, blue);
        }

        return null;
    }

}
